package cron.core.loader;

import cron.task.plugin.Task;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

/**
 * @author : willian fu
 * @version : 1.0
 * @date : 2021/2/25 0025 14:52
 */
public class JStrTaskResourceLoader implements Task {

    private String javaScriptStr;

    private static final ScriptEngine engine = new ScriptEngineManager().getEngineByName("javascript");

    public JStrTaskResourceLoader(String javaScriptStr) {
        this.javaScriptStr = javaScriptStr;
    }

    /**
     * 执行任务逻辑，插件启动入口
     */
    @Override
    public void doRun() {
        try {
            engine.eval(javaScriptStr);
            Invocable inv = (Invocable) engine;
            Object res = inv.invokeFunction("doRun");
        } catch (ScriptException | NoSuchMethodException e) {
            e.printStackTrace();
        }
    }
}
